package com.pls.action;

import java.sql.SQLException;
import java.util.ArrayList;

import org.apache.log4j.Logger;

import java.util.List;

import com.pls.actionObjects.GetSeatCodeByFloorRequest;
import com.pls.actionObjects.GetSeatCodeByFloorResponse;
import com.pls.actionObjects.GetSeatMapByFloorRequest;
import com.pls.actionObjects.GetSeatMapByFloorResponse;
import com.pls.constants.ValidationErrorEnum;
import com.pls.dao.DAOFactory;
import com.pls.dao.SeatDAO;
import com.pls.domain.EmployeeWorkstation;
import com.pls.domain.ExceptionError;
import com.pls.domain.ValidationError;


public class GetSeatMapByFloorHandler extends
		BaseRequestHandler<GetSeatMapByFloorResponse, GetSeatMapByFloorRequest> {

	Logger logger = Logger.getLogger(GetSeatMapByFloorHandler.class);

	public GetSeatMapByFloorHandler(Class<GetSeatMapByFloorResponse> tResponse) {
		super(tResponse);
		// TODO Auto-generated constructor stub
	}

	protected ArrayList<ValidationError> validate(
			GetSeatMapByFloorRequest request) {
		ArrayList<ValidationError> errors = new ArrayList<ValidationError>();
		ValidationError valError;
		if (request == null) {
			valError = new ValidationError();
			valError.setMessage("Request is null.");
			valError.setErrorName(ValidationErrorEnum.NullRequest);
			errors.add(valError);
			return errors;
		}

		return errors;
	}

	public GetSeatMapByFloorResponse process(GetSeatMapByFloorRequest request) {

		GetSeatMapByFloorResponse response = new GetSeatMapByFloorResponse();
		List<EmployeeWorkstation> employeeWorkstation;
		
		// call data layer
		SeatDAO seatDAO = DAOFactory.getSeatDAO();


		try {

			logger.debug("Process for GetSeatMapByFloorResponse entered.");

			employeeWorkstation = seatDAO.getSeatPlanByFloor(
					request.getLocation(), request.getFloor());
			response.setLocationSeatMap(employeeWorkstation);
			return response;

		} catch (SQLException e) {
			// ADD LOGGER
			ExceptionError sqlError = new ExceptionError(e.getClass()
					.toString(),
					"Sorry, an SQL error occurred while accessing the DB.");
			response.setExceptionError(sqlError);
		} catch (Exception e) {
			ExceptionError sqlError = new ExceptionError(e.getClass()
					.toString(),
					"Sorry, an error occurred while accessing the DB.");
			response.setExceptionError(sqlError);
		}
		return response;
	}
	public GetSeatCodeByFloorResponse process(GetSeatCodeByFloorRequest request) {

		GetSeatCodeByFloorResponse response = new GetSeatCodeByFloorResponse();
		List<EmployeeWorkstation> employeeWorkstation;
		
		// call data layer
		SeatDAO seatDAO = DAOFactory.getSeatDAO();


		try {

			logger.debug("Process for GetSeatCodeByFloorResponse entered.");

			employeeWorkstation = seatDAO.getSeatCodesByFloor(
					request.getLocation(), request.getFloor());
			response.setLocationSeatMap(employeeWorkstation);
			return response;

		} catch (SQLException e) {
			// ADD LOGGER
			ExceptionError sqlError = new ExceptionError(e.getClass()
					.toString(),
					"Sorry, an SQL error occurred while accessing the DB.");
			response.setExceptionError(sqlError);
		} catch (Exception e) {
			ExceptionError sqlError = new ExceptionError(e.getClass()
					.toString(),
					"Sorry, an error occurred while accessing the DB.");
			response.setExceptionError(sqlError);
		}
		return response;
	}

}
